//
//  RadioViewController.m
//  SunXi
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "RadioViewController.h"

@interface RadioViewController ()<RadioHomeTableViewHeadViewdelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, strong) UITableView *tableView;

@end

@implementation RadioViewController{
    NSMutableArray *_arr;
    NSInteger _loadnumber;
    UIView *_Sectioncview;
    NSTimer *_buttonTime;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.title = @"电台";
    SV.backgroundColor = [UIColor whiteColor];
    
    CGFloat  hei =  258 / (608 / WIDTH) + 10;
    
    RadioHomeTableViewHeadView *view = [[RadioHomeTableViewHeadView alloc]initWithFrame:CGRectMake(0, 0,WIDTH, ((WIDTH - 16) / 3) + hei)];
    view.delegate = self;
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    [SV addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(SV).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        
    }];
    
    
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.tableHeaderView = view;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//MARK:上拉加载;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _loadnumber += 9;
        [self addArrData];
    }];
//MARK:下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self createData];
        _loadnumber = 0;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"刷新轮播等" object:nil];
    }];
    
    self.tableView.rowHeight = 95;
    [self createData];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([RadioSavemodel shareSingleton].playlistArr.count > 0) {
        //右边按钮
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"zhuanpan"] style:UIBarButtonItemStylePlain target:self action:@selector(pushplaycon)];
        self.navigationItem.rightBarButtonItem.tintColor = BUTTONCOLOR;
        
    }
}


- (void)pushplaycon{
    RadioplayViewController *VC = [[RadioplayViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)createData{
    NSString *radioURL = @"http://api2.pianke.me/ting/radio";
    NSString *para = @"client=2";
    [DyAFnetWorking cachePostWithURL:radioURL WithbodyStr:para success:^(id result) {
        _arr = [NSMutableArray array];
        _arr = [RadioModel baseModelByArr:result[@"data"][@"alllist"]];
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(id nonet) {
        
        
    }];
    
    
}

//MARK:上拉加载方法
- (void)addArrData{
    NSString *radioUrl = @"http://api2.pianke.me/ting/radio_list";
    NSString *para = [NSString stringWithFormat:@"start=%ld&client=2&limit=9",_loadnumber];
    [DyAFnetWorking cachePostWithURL:radioUrl WithbodyStr:para success:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        for (NSDictionary *dic  in arr) {
            RadioModel *model = [[RadioModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_arr addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(id nonet) {
        
        
    }];
    

    
}
- (void)TouchScrolltakeValue:(id)TouchValue{
    NSArray *strArr = [TouchValue componentsSeparatedByString:@"/"];
    NSLog(@"%@",[strArr lastObject]);
    
    if ([TouchValue containsString:@"ting"]) {
        NSString *radioUrl = @"http://api2.pianke.me/ting/info";
        NSDictionary *para = @{@"tingid":[strArr lastObject],@"client":@"2"};
        [DyAFnetWorking POSTAfnNetworkingWithUrl:radioUrl WithParameters:para Block:^(id result) {
            RadioModel *model = [[RadioModel alloc]init];
            [model setValuesForKeysWithDictionary:result[@"data"][@"playInfo"]];
            RadioplayViewController *playVC = [[RadioplayViewController alloc]init];
            NSMutableArray *musicArr = [NSMutableArray array];
            [musicArr addObject:model];
            [RadioSavemodel shareSingleton].playlistArr = musicArr;
            [RadioSavemodel shareSingleton].playNumber = 0;
            [self.navigationController pushViewController:playVC animated:YES];
        }];
        
    }else if ([TouchValue containsString:@"fm"]){
        RadiolistViewController *listVC = [[RadiolistViewController alloc]init];
        RadioModel *model = [[RadioModel alloc]init];
        model.radioid = [strArr lastObject];
        listVC.radioid = model.radioid;
        listVC.title = @"加载中...";
        [self.navigationController pushViewController:listVC animated:YES];
       
    }

    
}


- (void)TouchCollectionViewtakeValue:(id)TouchValue{
    RadiolistViewController *listVC = [[RadiolistViewController alloc]init];
    RadioModel *model = TouchValue;
    listVC.radioid = model.radioid;
    listVC.title = @"加载中...";
    [self.navigationController pushViewController:listVC animated:YES];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioConTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dtcell"];
    if (!cell) {
        cell = [[RadioConTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"dtcell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    RadioModel *model = _arr[indexPath.row];
    cell.model = model;
    return cell;
}
//MARK:自定义分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//MARK:自定义分区视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    _Sectioncview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
    UILabel *labei = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 0, 20)];
    labei.text = @"全部电台 · All stations";
    labei.numberOfLines = 1;
    labei.font = [UIFont systemFontOfSize:13];
    [labei sizeToFit];
    [_Sectioncview addSubview:labei];
    UIView *xian = [[UIView alloc]initWithFrame:CGRectMake(labei.frame.size.width + 7, 9.5, WIDTH - (labei.frame.size.width + 2), 1)];
    xian.backgroundColor = [UIColor colorWithRed:0.031 green:0.900 blue:0.620 alpha:0.700];
    [_Sectioncview addSubview:xian];

    return _Sectioncview;
}
//MARK:分区标题高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
//MARK:table点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RadiolistViewController *listVC = [[RadiolistViewController alloc]init];
    RadioModel *model = _arr[indexPath.row];
    listVC.radioid = model.radioid;
    listVC.title = @"加载中...";
    [self.navigationController pushViewController:listVC animated:YES];
    
}

//MARK:隐藏标题视图
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y >= 290) {
        _Sectioncview.hidden = YES;
    }else{
        _Sectioncview.hidden = NO;
    }
}



@end
